package com.zz.examination.utils;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;

public class ZUtils {

    /**
     * 生成UUID
     *
     * @return
     */
    public static String getUUID() {
        return StringUtils.substring(UUID.randomUUID().toString().replace("-", ""), 0, 10);
    }

    /**
     * 生成n个不同的随机数，且随机数区间为[0,max)
     *
     * @param max 区间最大值
     * @param n   生成个数
     * @return
     */
    public static ArrayList<Integer> getDiffNO(int max, int n) {
        // 生成 [0-n) 个不重复的随机数
        // list 用来保存这些随机数
        ArrayList<Integer> list = new ArrayList<>();
        Random rand = new Random();
        boolean[] bool = new boolean[max];
        int num = 0;
        for (int i = 0; i < n; i++) {
            do {
                // 如果产生的数相同继续循环
                num = rand.nextInt(max);
            } while (bool[num]);
            bool[num] = true;
            list.add(num);
        }
        return list;
    }

    /**
     * 生成max个不同的随机数，且随机数区间为[0,max)
     *
     * @param max
     * @return
     */
    public static ArrayList<Integer> getDiffNO(int max) {
        return ZUtils.getDiffNO(max, max);
    }

    /**
     * 获取日期时间的字符串
     *
     * @param ldt
     * @return
     */
    public static String getDateTimeStr(LocalDateTime ldt) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String str = dtf.format(ldt);
        return str;
    }

    /**
     * 获取当前日期时间的字符串
     *
     * @return
     */
    public static String getNowDateTimeStr() {
        return ZUtils.getDateTimeStr(LocalDateTime.now());
    }

    static int count = 0;

    public static void main(String[] args) {
        ArrayList<String> baseList = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
        recursive(new ArrayList<>(), baseList);
    }

    public static void recursive(ArrayList<String> newList, ArrayList<String> baseList) {
        if (newList.size() == baseList.size()) {
            System.out.println(newList + "----" + ++count);
            return;
        }
        for (String str : baseList) {
            ArrayList<String> clone = (ArrayList<String>) newList.clone();
            clone.add(str);
            recursive(clone, baseList);
        }
    }
}
